#!/usr/bin/perl -w
# Eg,  $ ./foldSchoolItem.pl dir/*.txt >itemCombinedSchools.txt
# Careful: place the output file in such a way that it
# doesn't become one of the input files!

my $orgType = 'school';
foreach my $file (@ARGV) {
  $file =~ /(.*\/)?(....)(..)([^.]+)/;
  my $year = $2;
  my $grade = $3;
  my $subject = $4;
  open my $in, '<', $file or die "Can't open $file: $!";
  while (<$in>) {
    chomp;
    s/\r//;  # double-chomp
    # Separate district and school names.
    /^([^\t]+) - ([^\t]+)\t(\d{8})/;
    my $districtName = $1;
    my $schoolName = $2;
    s/^[^\t]+\t//;  # remove old combined name so we can closer match field order of the district file
    # Convert percentage figures to decimal fractions.
    s/(\s)(\d)%/${1}0.0$2/g;  # single-digit percentage
    s/(\s)(\d\d)%/${1}0.$2/g;  # double-digit percentage
    s/100%/1/g;  # triple-digit percentage  :-)
    # Zap &nbsp;
    s/&nbsp;//g;
    # Write it out.
    print $year, "\t", $grade, "\t", $subject, "\t", 
      $districtName, "\t", $_, "\t", $orgType, "\t", $schoolName, "\n";
  }
  close $in;
}
